/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PCR;

import Enums.ProjectType;
import components.JWorkArea;
import components.Marker;
import components.Wells;
import java.awt.Image;
import java.io.File;
import javax.swing.*;

/**
 *
 * @author marwa
 */
public class WorkArea extends javax.swing.JInternalFrame {

    JWorkArea bandsPanel;
    ///// create protein workarea
    int type;
    int maxLoops;
    String IType;
    Integer[] nextPos = {45, 110, 165, 190, 245, 290, 460, 560};
    Integer[] sample16Position = {5, 75, 145, 215, 285, 355, 425, 495, 565, 635, 705, 775, 845, 915, 985, 1055, 1125};
    Integer[] sample8Position = {3, 136, 269, 402, 535, 668, 801, 934, 1067};
    Integer[] sample4Position = {3, 255, 544, 833, 1122};
    private Integer sampleSize = 0;

    public Integer[] get16Position() {
        return sample16Position;
    }

    public Integer[] get4Position() {
        return sample4Position;
    }

    public Integer[] get8Position() {
        return sample8Position;
    }

    public void set16Position(Integer[] poses) {
        for (int i = 0; i < poses.length; i++) {
            sample16Position[i] = poses[i];
        }
    }

    /*
     * This Fuction used to return array containing pixels positions for 8
     * samples
     */
    public void set8Position(Integer[] poses) {
        for (int i = 0; i < poses.length; i++) {
            sample8Position[i] = poses[i];
        }
    }

    public void set4Position(Integer[] poses) {
        for (int i = 0; i < poses.length; i++) {
            sample4Position[i] = poses[i];
        }
    }

    public int getMarkerPos() {
        if (sampleSize == 16) {
            return get16Position()[0];
        } else if (sampleSize == 8) {
            return get8Position()[0];
        } else {
            return get4Position()[0];
        }
    }

    public void setSampleSize(int size) {
        sampleSize = size;
        Integer[] samples = new Integer[sampleSize];
        for (int i = 0; i < sampleSize; i++) {
            samples[i] = i + 1;
        }
        MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
        frm.getSampleCombo().setModel(new DefaultComboBoxModel(samples));
    }

    /**
     * Creates new form EightWorkArea
     */
    public WorkArea(String title, boolean newDocument, String imageType) {
        initComponents();
        this.setTitle(title);
        IType = imageType;
        if (newDocument) {
            bandsPanel = new JWorkArea(IType); //this is for the pcr panel
        } else {
            MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
            bandsPanel = frm.getOpenedWorkArea();
        }
        workAreaScrollPane.setViewportView(bandsPanel);


    }

    public void setWorkArea(JWorkArea warea) {
        bandsPanel = warea;
    }

    public JScrollPane getWorkAreaScrollPane() {
        return this.workAreaScrollPane;
    }

    /*
     * This function used to set marker type "ex: 50, 1000 ,etc.."
     */
    public void setMarkerType(int type, int numberOfSamples) {
        this.type = type;
        drawMarker(numberOfSamples);
    }
    boolean saved = false;

    public void setSaved(boolean state) {
        saved = state;
    }

    public boolean isSaved() {
        return saved;
    }
    private File savedPath = null;

    public void setSavedPath(File file) {
        savedPath = file;
    }

    public File getSavedPath() {
        return savedPath;
    }
    /*
     * This function used to get marker type
     */

    public int getMarkerType() {
        return type;
    }
    int pos;

    public void setMarkerPos(int pos) {
        this.pos = pos;
    }
    private boolean openedProject = false;

    public void setOpenedProject(boolean bool) {
        openedProject = bool;
    }

    public boolean isOpenedProject() {
        return openedProject;
    }
    private File projectPath;

    public void setProjectPath(File file) {
        projectPath = file;
    }

    public File getProjectPath() {
        return projectPath;
    }
    private ProjectType projectType;

    public void setProjectType(ProjectType pType) {
        projectType = pType;
    }

    public ProjectType getProjectType() {
        return projectType;
    }

    /*
     * This method is to draw the protein markers
     */
    public void drawProteinMarker(String type, int numberOfSamples, int propos) {
        int width = 0, height = 30;
        if (numberOfSamples == 4) {
            width = 120;
        } else if (numberOfSamples == 8) {
            width = 130;
        }
        for (int i = 1; i <= 8; i++) {
            Marker band = new Marker();
            String imgPath = "resources/markers/" + i + ".png";
            File file = new File(imgPath);
            if (file.exists()) {
                Icon icn = new javax.swing.ImageIcon(imgPath);
                band.setName(imgPath);
                Image img = ((ImageIcon) icn).getImage();
                ImageIcon icon = new ImageIcon(img.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING));
                band.setImage(icon);
                band.setSize(width, height);
                band.setVisible(true);
                band.setLocation(propos, nextPos[i - 1]);
                bandsPanel.add(band);
                bandsPanel.repaint();
            }
        }
    }

    /*
     * This function is to draw the wheel bands to the work area
     */
    public void wheelBandsDrawer(int number) {
        int width = 0, height = 30;
        Integer[] pos;
        MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
        if (number == 4) {
            pos = this.get4Position();
            width = 120;
        } else if (number == 8) {
            pos = this.get8Position();
            width = 130;
        } else {
            pos = this.get16Position();
            width = 67;
        }
        if (IType.equals("MRNA")) {
            maxLoops = number;
        } else if (IType.equals("PCR")) {
            maxLoops = number + 1;
        }
        for (int i = 0; i < maxLoops; i++) {
            Wells wheelShape = new Wells();
            String imgPath = "resources/wheels/1.png";
            File file = new File(imgPath);
            if (file.exists()) {
                Icon icn = new javax.swing.ImageIcon(imgPath);
                wheelShape.setName(imgPath);
                Image img = ((ImageIcon) icn).getImage();
                ImageIcon icon = new ImageIcon(img.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING));
                wheelShape.setImage(icon);
                wheelShape.setSize(width, height);
                wheelShape.setVisible(true);
                wheelShape.setLocation(pos[i], 10);
                bandsPanel.add(wheelShape);
                bandsPanel.repaint();
            }
        }
    }
    /*
     * This function used to draw marker in specified postion on the panel
     */

    private void drawMarker(int numberOfSamples) {
        int width = 0, height = 30;
        if (numberOfSamples == 4) {
            width = 120;
        } else if (numberOfSamples == 8) {
            width = 130;
        } else {
            width = 67;
        }
        for (int i = 1; i <= 8;
                i++) {
            Marker band = new Marker();
            String imgPath = "resources/markers/" + i + ".png";
            File file = new File(imgPath);
            if (file.exists()) {
                Icon icn = new javax.swing.ImageIcon(imgPath);
                band.setName(imgPath);
                Image img = ((ImageIcon) icn).getImage();
                ImageIcon icon = new ImageIcon(img.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING));
                band.setImage(icon);
                band.setSize(width, height);
                band.setVisible(true);
                band.setLocation(pos, nextPos[i - 1]);
                bandsPanel.add(band);
                bandsPanel.repaint();
            }
        }
    }

    public JWorkArea getBandsPanel() {
        return bandsPanel;
    }

    public void closeWindow() {
        MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
        if (!this.isSaved()) {
            int result = JOptionPane.showConfirmDialog(this, "Do you want to save this file?", "Save", JOptionPane.YES_NO_OPTION);
            switch (result) {
                case JOptionPane.YES_OPTION:
                    frm.getSaveAsButton().doClick();
                    return;
            }
        }

        if(projectType == ProjectType.MRNA)
        {
            frm.getDualButton().setEnabled(false);
        }
        frm.getToolbox().setToolsEnabled(false);
        frm.setMenuShapeToolsEnabled(false);
        frm.getOpenedFiles().remove(savedPath);
        frm.getPositioningToolbar().setVisible(false);
        frm.setEditingButtonsEnable(false);
        frm.setSelectedWorkArea(null);
        int num = frm.getOpenedWorkAreaNum() - 1;
        frm.setOpenedWorkAreaNum(num);
        this.dispose();
    }

    @Override
    public void doDefaultCloseAction() {
        closeWindow();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        workAreaScrollPane = new javax.swing.JScrollPane();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameActivated(evt);
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameDeiconified(evt);
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameIconified(evt);
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(workAreaScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 812, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(workAreaScrollPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 606, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameActivated(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameActivated
        // TODO add your handling code here:        
        MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
        frm.setEditingButtonsEnable(true);
        frm.setSelectedWorkArea(this);
        frm.getToolbox().initializeTools(projectType);
        frm.resetMenuShapeTools(projectType);
        frm.getPositioningToolbar().setVisible(true);
        frm.getCompanyDataBtn().setEnabled(false);
        frm.getLogoBtn().setEnabled(false);
        if(projectType == ProjectType.MRNA)
        {
            frm.getDualButton().setEnabled(true);
        }
    }//GEN-LAST:event_formInternalFrameActivated

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
        int num = frm.getOpenedWorkAreaNum() + 1;
        frm.setOpenedWorkAreaNum(num);
        frm.setEditingButtonsEnable(true);
        frm.getPositioningToolbar().setVisible(true);
        frm.getCompanyDataBtn().setEnabled(false);
        frm.getLogoBtn().setEnabled(false);
    }//GEN-LAST:event_formInternalFrameOpened

    private void formInternalFrameIconified(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameIconified
        // TODO add your handling code here:
        MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
        frm.setMenuShapeToolsEnabled(false);
        frm.getToolbox().setToolsEnabled(false);
        frm.getPositioningToolbar().setVisible(false);
        boolean flag = false;
        for (JInternalFrame jf : frm.getDesktopPane().getAllFrames()) {
            if (!jf.isIcon() && (jf.getClass().getName().equals("PCR.WorkArea")
                    || jf.getClass().getName().equals("PCR.Sequencer"))) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            frm.setEditingButtonsEnable(false);
        }
        if(projectType == ProjectType.MRNA)
        {
            frm.getDualButton().setEnabled(false);
        }
    }//GEN-LAST:event_formInternalFrameIconified

    private void formInternalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameDeiconified
        // TODO add your handling code here:
        MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
        frm.setEditingButtonsEnable(true);
        frm.setSelectedWorkArea(this);
        frm.getPositioningToolbar().setVisible(true);
        frm.getToolbox().initializeTools(projectType);
        frm.resetMenuShapeTools(projectType);
        if(projectType == ProjectType.MRNA)
        {
            frm.getDualButton().setEnabled(true);
        }
    }//GEN-LAST:event_formInternalFrameDeiconified
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane workAreaScrollPane;
    // End of variables declaration//GEN-END:variables
}
